package com.yangcangclub.user.component;

import com.yangcangclub.commons.config.SysConfig;
import com.yangcangclub.commons.result.ResultData;
import com.yangcangclub.commons.utils.HttpUtils;
import com.yangcangclub.commons.utils.JsonUtils;
import com.yangcangclub.commons.utils.ObjectUtils;
import com.yangcangclub.commons.utils.StringUtil;
import com.google.common.collect.Maps;
import com.yangcangclub.user.bean.OauthUser;
import com.yangcangclub.user.domain.UserProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class UserComponent {

    @Autowired
    private SysConfig sysConfig;


    @Autowired
    private UserProfile userProfile;
    @Autowired
    RedisTemplate redisTemplate;

    public Map<String,Object> getUserFullInfoByAccessToken(String access_token){

        Map<String,Object> map = Maps.newHashMap();

        if(StringUtil.isEmpty(access_token)) {
            return null; //invalid access_token
        }

        //FIXME traveler api load user data from cahce ,so send request per yangcangclub request,no cahce here!
        Map<String,String> params= Maps.newHashMap();
        params.put("access_token",access_token);
        String result= HttpUtils.doGet(sysConfig.getAjaxUrlHead() + sysConfig.getUserAccessTokenUrl(), params);
        ResultData resultData =(ResultData) JsonUtils.parseObject(result, ResultData.class);
        int responseCode = resultData.getCode();
        if(1000!=responseCode) {
            //TODO return user access_token invaild
            return null;
        } else {
            Map<String,Object> m= (Map)resultData.getData();
            if(m.size()>0&&null!=m.get("userInfo")&&null!=m.get("privilege")) {
//                    User remoteUserInfo = (User)m.get("userInfo");
//                    Set<String> remotePrivilege= (Set<String>)m.get("privilege");
                map=m;
            } else {
                //TODO return user access_token invaild
                return null;
            }
        }

        //本机cache获取access_token对应的用户id
//        Object obj= redisTemplate.boundValueOps("user.access_token." + access_token+".id").get();
//        if(obj==null) {
//
//
//
//            int userId=(Integer)obj;
//            BoundValueOperations bo=redisTemplate.boundValueOps("user.id."+userId+".info");
//            User user= JSON.parseObject(bo.get().toString(), User.class);
//            user.setAccessToken(access_token);
//
//            BoundSetOperations privisBo=redisTemplate.boundSetOps("user.access_token." + access_token + ".privilege");
//            Set<String> set= privisBo.members();
//            map.put("userInfo",user);
//            map.put("privilege",set);
//
//            return map;
//        }
        return map;
    }

    /**
     * 根据用户ID获得用户第三方登录Openid
     * @param userId
     * @return
     */
    public  Map<String,Object> getWxOpenIdByUserId(int userId){
        Map<String,Object> map = Maps.newHashMap();
        OauthUser oauthUser = userProfile.getWxOpenIdByUserId(userId);
        if(ObjectUtils.isNotNull(oauthUser)){
            map.put("userId",oauthUser.getUser_id());
            map.put("openId",oauthUser.getOauth_openid());
        }
        return map;
    }

    /**
     * 根据ID查询用户信息
     * @param id
     * @return
     */
    public  Map<String,Object> getUserInfo(int id){
        Map<String,Object> map = Maps.newHashMap();

        Map<String,String> params= Maps.newHashMap();
        params.put("userId",id+"");
        String result= HttpUtils.doGet(sysConfig.getAjaxUrlHead() + sysConfig.getUserInfoUrl(), params);
        ResultData resultData =(ResultData) JsonUtils.parseObject(result, ResultData.class);
        int responseCode = resultData.getCode();
        if(1000==responseCode) {
            Object userInfo = resultData.getData();
            map.put("userInfo",userInfo);
        }
        return map;
    }


    /**
     * 根据IDs批量查询用户列表：ids=1,2,3
     * @return
     */
    public  Map<String,Object> getUserList(String ids){
        Map<String,Object> map = Maps.newHashMap();

        Map<String,String> params= Maps.newHashMap();
        params.put("ids",ids);
        String result= HttpUtils.doGet(sysConfig.getAjaxUrlHead() + sysConfig.getUserListUrl(), params);
        ResultData resultData =(ResultData) JsonUtils.parseObject(result, ResultData.class);
        int responseCode = resultData.getCode();
        if(1000==responseCode) {
            Object userList = resultData.getData();
            map.put("userList",userList);
        }
        return map;
    }




}
